﻿namespace Common
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml;

    public class DeviceOptionsDialog : Form
    {
        private Button Cancel_Button;
        private Label ChooseDevice_Label;
        public string filename;
        private RadioButton Fullscreen_Radio;
        public int fulscreen_rate = 0x4b;
        public int fulscreen_x = 640;
        public int fulscreen_y = 480;
        private Button OK_Button;
        private ComboBox RefreshRate_Box;
        private ComboBox Size_Box;
        private NumericUpDown Size_x_UpDown;
        private NumericUpDown Size_y_UpDown;
        public bool windowed;
        private RadioButton Windowed_Radio;
        public int windowed_x = 640;
        public int windowed_y = 480;

        public DeviceOptionsDialog(string filename)
        {
            this.InitializeComponent();
            this.filename = filename;
            if (File.Exists(filename))
            {
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlElement documentElement = document.DocumentElement;
                if (documentElement["fulscreen_x"] != null)
                {
                    this.fulscreen_x = int.Parse(documentElement["fulscreen_x"].InnerText);
                }
                if (documentElement["fulscreen_y"] != null)
                {
                    this.fulscreen_y = int.Parse(documentElement["fulscreen_y"].InnerText);
                }
                if (documentElement["fulscreen_rate"] != null)
                {
                    this.fulscreen_rate = int.Parse(documentElement["fulscreen_rate"].InnerText);
                }
                if (documentElement["windowed_x"] != null)
                {
                    this.windowed_x = int.Parse(documentElement["windowed_x"].InnerText);
                }
                if (documentElement["windowed_y"] != null)
                {
                    this.windowed_y = int.Parse(documentElement["windowed_y"].InnerText);
                }
                if (documentElement["windowed"] != null)
                {
                    this.windowed = bool.Parse(documentElement["windowed"].InnerText);
                }
            }
            if ((this.fulscreen_x == 320) && (this.fulscreen_y == 240))
            {
                this.Size_Box.SelectedIndex = 0;
            }
            if ((this.fulscreen_x == 640) && (this.fulscreen_y == 480))
            {
                this.Size_Box.SelectedIndex = 1;
            }
            if ((this.fulscreen_x == 800) && (this.fulscreen_y == 600))
            {
                this.Size_Box.SelectedIndex = 2;
            }
            if ((this.fulscreen_x == 0x400) && (this.fulscreen_y == 0x300))
            {
                this.Size_Box.SelectedIndex = 3;
            }
            if ((this.fulscreen_x == 0x480) && (this.fulscreen_y == 0x360))
            {
                this.Size_Box.SelectedIndex = 4;
            }
            if ((this.fulscreen_x == 0x500) && (this.fulscreen_y == 800))
            {
                this.Size_Box.SelectedIndex = 5;
            }
            if ((this.fulscreen_x == 0x500) && (this.fulscreen_y == 960))
            {
                this.Size_Box.SelectedIndex = 6;
            }
            if ((this.fulscreen_x == 0x500) && (this.fulscreen_y == 0x400))
            {
                this.Size_Box.SelectedIndex = 7;
            }
            if ((this.fulscreen_x == 0x640) && (this.fulscreen_y == 0x4b0))
            {
                this.Size_Box.SelectedIndex = 8;
            }
            if ((this.fulscreen_x == 0x690) && (this.fulscreen_y == 0x41a))
            {
                this.Size_Box.SelectedIndex = 9;
            }
            if (this.fulscreen_rate == 60)
            {
                this.RefreshRate_Box.SelectedIndex = 0;
            }
            if (this.fulscreen_rate == 0x4b)
            {
                this.RefreshRate_Box.SelectedIndex = 1;
            }
            if (this.fulscreen_rate == 0x55)
            {
                this.RefreshRate_Box.SelectedIndex = 2;
            }
            if (this.fulscreen_rate == 100)
            {
                this.RefreshRate_Box.SelectedIndex = 3;
            }
            this.Size_x_UpDown.Value = this.windowed_x;
            this.Size_y_UpDown.Value = this.windowed_y;
            if (this.windowed)
            {
                this.Windowed_Radio.Checked = true;
            }
            else
            {
                this.Fullscreen_Radio.Checked = true;
            }
            this.Box_SelectedIndexChanged(this, new EventArgs());
        }

        private void Box_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OK_Button.Enabled = true;
            if (this.Fullscreen_Radio.Checked && ((this.Size_Box.SelectedIndex < 0) || (this.RefreshRate_Box.SelectedIndex < 0)))
            {
                this.OK_Button.Enabled = false;
            }
        }

        private void InitializeComponent()
        {
            this.Fullscreen_Radio = new RadioButton();
            this.Windowed_Radio = new RadioButton();
            this.ChooseDevice_Label = new Label();
            this.Size_Box = new ComboBox();
            this.RefreshRate_Box = new ComboBox();
            this.Size_x_UpDown = new NumericUpDown();
            this.Size_y_UpDown = new NumericUpDown();
            this.OK_Button = new Button();
            this.Cancel_Button = new Button();
            this.Size_x_UpDown.BeginInit();
            this.Size_y_UpDown.BeginInit();
            base.SuspendLayout();
            this.Fullscreen_Radio.Location = new Point(0x18, 0x34);
            this.Fullscreen_Radio.Name = "Fullscreen_Radio";
            this.Fullscreen_Radio.Size = new Size(0x68, 0x15);
            this.Fullscreen_Radio.TabIndex = 0;
            this.Fullscreen_Radio.Text = "Fullscreen";
            this.Fullscreen_Radio.CheckedChanged += new EventHandler(this.Radio_CheckedChanged);
            this.Windowed_Radio.Location = new Point(0x18, 0x4d);
            this.Windowed_Radio.Name = "Windowed_Radio";
            this.Windowed_Radio.Size = new Size(0x68, 0x15);
            this.Windowed_Radio.TabIndex = 1;
            this.Windowed_Radio.Text = "Windowed";
            this.Windowed_Radio.CheckedChanged += new EventHandler(this.Radio_CheckedChanged);
            this.ChooseDevice_Label.AutoSize = true;
            this.ChooseDevice_Label.Location = new Point(14, 0x11);
            this.ChooseDevice_Label.Name = "ChooseDevice_Label";
            this.ChooseDevice_Label.Size = new Size(0x5d, 0x11);
            this.ChooseDevice_Label.TabIndex = 2;
            this.ChooseDevice_Label.Text = "Choose Device:";
            this.Size_Box.DropDownStyle = ComboBoxStyle.DropDownList;
            this.Size_Box.Items.AddRange(new object[] { "320x240", "640x480", "800x600", "1024x768", "1152x864", "1280x800", "1280x960", "1280x1024", "1600x1200", "1680x1050" });
            this.Size_Box.Location = new Point(130, 50);
            this.Size_Box.Name = "Size_Box";
            this.Size_Box.Size = new Size(90, 0x15);
            this.Size_Box.TabIndex = 3;
            this.Size_Box.SelectedIndexChanged += new EventHandler(this.Box_SelectedIndexChanged);
            this.RefreshRate_Box.DropDownStyle = ComboBoxStyle.DropDownList;
            this.RefreshRate_Box.Items.AddRange(new object[] { "60hz", "75hz", "85hz", "100hz" });
            this.RefreshRate_Box.Location = new Point(0xe1, 50);
            this.RefreshRate_Box.Name = "RefreshRate_Box";
            this.RefreshRate_Box.Size = new Size(0x3d, 0x15);
            this.RefreshRate_Box.TabIndex = 5;
            this.RefreshRate_Box.SelectedIndexChanged += new EventHandler(this.Box_SelectedIndexChanged);
            int[] bits = new int[4];
            bits[0] = 40;
            this.Size_x_UpDown.Increment = new decimal(bits);
            this.Size_x_UpDown.Location = new Point(130, 0x4d);
            int[] numArray2 = new int[4];
            numArray2[0] = 0xfa0;
            this.Size_x_UpDown.Maximum = new decimal(numArray2);
            int[] numArray3 = new int[4];
            numArray3[0] = 320;
            this.Size_x_UpDown.Minimum = new decimal(numArray3);
            this.Size_x_UpDown.Name = "Size_x_UpDown";
            this.Size_x_UpDown.Size = new Size(50, 0x15);
            this.Size_x_UpDown.TabIndex = 6;
            int[] numArray4 = new int[4];
            numArray4[0] = 640;
            this.Size_x_UpDown.Value = new decimal(numArray4);
            int[] numArray5 = new int[4];
            numArray5[0] = 30;
            this.Size_y_UpDown.Increment = new decimal(numArray5);
            this.Size_y_UpDown.Location = new Point(0xb9, 0x4d);
            int[] numArray6 = new int[4];
            numArray6[0] = 0xbb8;
            this.Size_y_UpDown.Maximum = new decimal(numArray6);
            int[] numArray7 = new int[4];
            numArray7[0] = 240;
            this.Size_y_UpDown.Minimum = new decimal(numArray7);
            this.Size_y_UpDown.Name = "Size_y_UpDown";
            this.Size_y_UpDown.Size = new Size(50, 0x15);
            this.Size_y_UpDown.TabIndex = 7;
            int[] numArray8 = new int[4];
            numArray8[0] = 480;
            this.Size_y_UpDown.Value = new decimal(numArray8);
            this.OK_Button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.OK_Button.DialogResult = DialogResult.OK;
            this.OK_Button.Location = new Point(0x62, 120);
            this.OK_Button.Name = "OK_Button";
            this.OK_Button.TabIndex = 8;
            this.OK_Button.Text = "OK";
            this.OK_Button.Click += new EventHandler(this.OK_Button_Click);
            this.Cancel_Button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.Cancel_Button.DialogResult = DialogResult.Cancel;
            this.Cancel_Button.Location = new Point(0xba, 120);
            this.Cancel_Button.Name = "Cancel_Button";
            this.Cancel_Button.TabIndex = 8;
            this.Cancel_Button.Text = "Cancel";
            base.AcceptButton = this.OK_Button;
            this.AutoScaleBaseSize = new Size(6, 14);
            base.CancelButton = this.Cancel_Button;
            base.ClientSize = new Size(0x132, 0xa1);
            base.ControlBox = false;
            base.Controls.Add(this.OK_Button);
            base.Controls.Add(this.Size_y_UpDown);
            base.Controls.Add(this.Size_x_UpDown);
            base.Controls.Add(this.RefreshRate_Box);
            base.Controls.Add(this.Size_Box);
            base.Controls.Add(this.ChooseDevice_Label);
            base.Controls.Add(this.Windowed_Radio);
            base.Controls.Add(this.Fullscreen_Radio);
            base.Controls.Add(this.Cancel_Button);
            this.Font = new Font("Verdana", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0xcc);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Name = "DeviceOptionsDialog";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Device Options";
            this.Size_x_UpDown.EndInit();
            this.Size_y_UpDown.EndInit();
            base.ResumeLayout(false);
        }

        private void OK_Button_Click(object sender, EventArgs e)
        {
            switch (this.Size_Box.SelectedIndex)
            {
                case 0:
                    this.fulscreen_x = 320;
                    this.fulscreen_y = 240;
                    break;

                case 1:
                    this.fulscreen_x = 640;
                    this.fulscreen_y = 480;
                    break;

                case 2:
                    this.fulscreen_x = 800;
                    this.fulscreen_y = 600;
                    break;

                case 3:
                    this.fulscreen_x = 0x400;
                    this.fulscreen_y = 0x300;
                    break;

                case 4:
                    this.fulscreen_x = 0x480;
                    this.fulscreen_y = 0x360;
                    break;

                case 5:
                    this.fulscreen_x = 0x500;
                    this.fulscreen_y = 800;
                    break;

                case 6:
                    this.fulscreen_x = 0x500;
                    this.fulscreen_y = 960;
                    break;

                case 7:
                    this.fulscreen_x = 0x500;
                    this.fulscreen_y = 0x400;
                    break;

                case 8:
                    this.fulscreen_x = 0x640;
                    this.fulscreen_y = 0x4b0;
                    break;

                case 9:
                    this.fulscreen_x = 0x690;
                    this.fulscreen_y = 0x41a;
                    break;
            }
            switch (this.RefreshRate_Box.SelectedIndex)
            {
                case 0:
                    this.fulscreen_rate = 60;
                    break;

                case 1:
                    this.fulscreen_rate = 0x4b;
                    break;

                case 2:
                    this.fulscreen_rate = 0x55;
                    break;

                case 3:
                    this.fulscreen_rate = 100;
                    break;
            }
            this.windowed_x = (int) this.Size_x_UpDown.Value;
            this.windowed_y = (int) this.Size_y_UpDown.Value;
            this.windowed = this.Windowed_Radio.Checked;
            XmlDocument document = new XmlDocument();
            document.LoadXml("<Options />");
            XmlNode newChild = document.CreateNode(XmlNodeType.Element, "fulscreen_x", "");
            newChild.InnerText = this.fulscreen_x.ToString();
            document.DocumentElement.AppendChild(newChild);
            newChild = document.CreateNode(XmlNodeType.Element, "fulscreen_y", "");
            newChild.InnerText = this.fulscreen_y.ToString();
            document.DocumentElement.AppendChild(newChild);
            newChild = document.CreateNode(XmlNodeType.Element, "fulscreen_rate", "");
            newChild.InnerText = this.fulscreen_rate.ToString();
            document.DocumentElement.AppendChild(newChild);
            newChild = document.CreateNode(XmlNodeType.Element, "windowed_x", "");
            newChild.InnerText = this.windowed_x.ToString();
            document.DocumentElement.AppendChild(newChild);
            newChild = document.CreateNode(XmlNodeType.Element, "windowed_y", "");
            newChild.InnerText = this.windowed_y.ToString();
            document.DocumentElement.AppendChild(newChild);
            newChild = document.CreateNode(XmlNodeType.Element, "windowed", "");
            newChild.InnerText = this.windowed.ToString();
            document.DocumentElement.AppendChild(newChild);
            document.Save(this.filename);
        }

        private void Radio_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Fullscreen_Radio.Checked)
            {
                this.Size_Box.Enabled = true;
                this.RefreshRate_Box.Enabled = true;
            }
            else
            {
                this.Size_Box.Enabled = false;
                this.RefreshRate_Box.Enabled = false;
            }
            if (this.Windowed_Radio.Checked)
            {
                this.Size_x_UpDown.Enabled = true;
                this.Size_y_UpDown.Enabled = true;
            }
            else
            {
                this.Size_x_UpDown.Enabled = false;
                this.Size_y_UpDown.Enabled = false;
            }
            this.Box_SelectedIndexChanged(sender, e);
        }
    }
}

